package com.yanchangapp.ui.presenter;
import com.zxl.baselib.bean.response.BaseResponse;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.ui.base.BaseObserver;
import com.zxl.baselib.ui.base.BasePresenter;
import com.zxl.baselib.ui.base.ExceptionHandle;
import com.yanchangapp.base.HeadApiRetrofit;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.model.request.SaveDraftRequest;
import com.yanchangapp.model.request.SendMailRequest;
import com.yanchangapp.ui.view.MailWriteAtView;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class MailWriteAtPresenter extends BasePresenter<MailWriteAtView> {

    public MailWriteAtPresenter(BaseActivity context) {
        super(context);
    }

    public void sendMail(SendMailRequest sendMailRequest){
        if (getView() != null){
            getView().showLoadingDialog();
        }
        HeadApiRetrofit.getInstance().sendMail(sendMailRequest)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        if (getView() != null){
                            getView().showLoadingDialog();
                        }
                        if (baseResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                            if (getView() != null){
                                getView().sendMailSuccess();
                            }
                        }else {
                            if (getView() != null){
                                getView().sendMailFailure(baseResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().showLoadingDialog();
                            getView().sendMailFailure("发送失败");
                        }
                    }
                });
    }


    // 保存草稿
    public void saveDraftMail(SaveDraftRequest saveDraftRequest){
        if (getView() != null){
            getView().showLoadingDialog();
        }
        HeadApiRetrofit.getInstance().saveMailToDraft(saveDraftRequest)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                       if (getView() != null){
                           getView().hideLoadingDialog();
                       }
                       if (baseResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                           if (getView() != null){
                               getView().saveDraftSuccess();
                           }
                       }else {
                           if (getView() != null){
                               getView().hideLoadingDialog();
                               getView().saveDraftFailure(baseResponse.getMsg());
                           }
                       }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().hideLoadingDialog();
                            getView().saveDraftFailure("保存草稿失败");
                        }
                    }
                });
    }
}
